﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using OGAMRL.screens;

namespace OGAMRL.units
{
    public class Player : GameObject
    {
        private int health;
        //player needs a reference to the gamestate manager so the state can changed based on player actions.
        private GameState gs;
        private Vector2 velocity;
        //private GameTime gt;
        private float moveRate = 10.0f;
        private float distanceMoved = 0.0f;
        //time to hold down key for rapid movement in ms
        private int moveHoldDelay = 300;
        private int buttonPressedTime = 0;
        private bool isMovingRapidly = false;
        private bool moved = false;
        private int lastTime = 0;

        public Player(Vector2 v, Texture2D t, ref GameState g)
            : base(v, t)
        {
            this.health = 100;
            this.gs = g;
            //gt = new GameTime();
        }
        private void updatePosition(GameTime t)
        {
            //move towards vector 
            this.position.X += velocity.X;
            this.position.Y += velocity.Y;        
            //increment total distance
            distanceMoved += Math.Abs(velocity.X) + Math.Abs(velocity.Y);
            //check distance moved against tile length
            if (distanceMoved >= this.getTexture().Width)
            {
                distanceMoved = 0.0f;
                velocity = Vector2.Zero;
                snapToGrid();
                moved = true;
            }
            if (isMovingRapidly)
                moved = false;
        }
        private void snapToGrid()
        {
            int gridHeight = this.getTexture().Height;
            int gridWidth = this.getTexture().Width;

            position.X = (int)Math.Floor(position.X);
            position.Y = (int)Math.Floor(position.Y);
        }
        public void readKeys(GameTime t)
        {
            #region clusterfuck of IFs
            KeyboardState ks = Keyboard.GetState();
            lastTime = t.ElapsedGameTime.Milliseconds;
            if (!moved)
            {
                if (ks.IsKeyDown(Keys.NumPad8))
                {
                    velocity = new Vector2(0.0f, -moveRate);
                }
                if (ks.IsKeyDown(Keys.NumPad2))
                {
                    velocity = new Vector2(0.0f, moveRate);
                }
                if (ks.IsKeyDown(Keys.NumPad4))
                {
                    velocity = new Vector2(-moveRate, 0.0f);
                }
                if (ks.IsKeyDown(Keys.NumPad6))
                {
                    velocity = new Vector2(moveRate, 0.0f);
                }
                if (ks.IsKeyDown(Keys.NumPad7))
                {
                    velocity = new Vector2(-moveRate, -moveRate);
                }
                if (ks.IsKeyDown(Keys.NumPad9))
                {
                    velocity = new Vector2(moveRate, -moveRate);
                }
                if (ks.IsKeyDown(Keys.NumPad1))
                {
                    velocity = new Vector2(-moveRate, moveRate);
                }
                if (ks.IsKeyDown(Keys.NumPad3))
                {
                    velocity = new Vector2(moveRate, moveRate);
                } 
            }
            if (ks.GetPressedKeys().Length <= 0)
            {
                //System.Diagnostics.Debug.Print(ks.GetPressedKeys().Length.ToString());
                buttonPressedTime = 0;
                isMovingRapidly = false;
                moved = false;
            }
            else
            {
                buttonPressedTime += lastTime;
            }
            if (buttonPressedTime >= moveHoldDelay)
            {
                isMovingRapidly = true;
            }     
            updatePosition(t);
            System.Diagnostics.Debug.Print(buttonPressedTime.ToString());
            #endregion
        }
    }
}
